﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Facebook.Client;
using System.Threading.Tasks;
using VideoPlay.PhoneClient.ViewModels;

namespace VideoPlay.PhoneClient.Views
{
    public partial class VideoShareFBView : PhoneApplicationPage
    {
        public VideoShareFBView()
        {
            InitializeComponent();
            this.Loaded += FacebookLoginPage_Loaded;
        }

        async void FacebookLoginPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.isAuthenticated)
            {
                App.isAuthenticated = true;
                await Authenticate();
            }
        }

        private FacebookSession session;
        private async Task Authenticate()
        {
            string message = String.Empty;
            try
            {
                session = await App.FacebookSessionClient.LoginAsync("user_about_me,read_stream");
                App.AccessToken = session.AccessToken;
                App.FacebookId = session.FacebookId;

                Dispatcher.BeginInvoke(this.ShareVideo);
            }
            catch (InvalidOperationException e)
            {
                message = "Login failed! Exception details: " + e.Message;
                MessageBox.Show(message);
            }
        }

        private async void ShareVideo()
        {
            Facebook.FacebookClient fbClient = new Facebook.FacebookClient();
            var parameters = new Dictionary<string, object>();
            parameters["message"] = "Testing 123";
            //parameters["url"] = "http://youtube.com";
            await fbClient.PostTaskAsync(parameters);

            ((VideoShareFBViewModel)DataContext).NavigationService.GoBack();
        }

    }
}